import { useUserStore } from '@/stores'
// import { useRouter } from 'vue-router'
// const route = useRoute()
import {
  createRouter,
  createWebHistory //不带#
  // createWebHashHistory //地址带#
} from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL), //跳路径都带前缀，在vite配
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/',
      component: () => import('@/views/homepage/Homepage.vue'),

      redirect: '/login', //默认访问管理页面
      children: [
        {
          path: 'article/manage',
          component: () => import('@/views/article/ArticleManage.vue')
        },
        {
          path: 'article/sort',
          component: () => import('@/views/article/ArticleSort.vue')
        },
        {
          path: 'user/data',
          component: () => import('@/views/user/BasicData.vue')
        },
        {
          path: 'user/avatar',
          component: () => import('@/views/user/ReplaceAvatar.vue')
        },
        {
          path: 'user/password',
          component: () => import('@/views/user/ResetPassword.vue')
        }
      ]
    }
  ]
})

// 登陆拦截  返回值：undefined/true放行
router.beforeEach((to) => {
  // 如果没有登陆，无token 且访问的是非登录页，拦截到登陆页面
  // 从用户仓库拿到token
  const useStore = useUserStore()
  if (!useStore.token && to.path !== '/login') return '/login'
})

export default router
